package com.ztc.ZtcTransportArea.web;

import com.aqgj.SysArea.bean.SysArea;
import com.aqgj.SysArea.service.SysAreaManager;
import com.aqgj.User.service.UserManager;
import com.aqgj.util.DateFarmatUtils;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.StringUtils;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import com.ztc.ZtcTransportArea.bean.ZtcTransportArea;
import com.ztc.ZtcTransportArea.service.ZtcTransportAreaManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("ztc/bindArea")
public class ZtcTransportAreaAction {

    @Autowired
    private ZtcTransportAreaManager ztcTransportAreaManager;
    @Autowired
    private SysAreaManager sysAreaManager;
    @Autowired
    private UserManager userManager;

    /**
     * 保存省级/地级公司所管理行政区域关系.
     *
     * @param cid  公司id
     * @param code 行政区域代码集
     * @return
     */
    @RequestMapping("saveZtcBindArea")
    @ResponseBody
    public HashMap<String, Object> saveZtcBindArea(String cid, String code) {
        if (StringUtils.isBlank(code)) {
            return JsonWrapper.failureWrapperMsg("请选择行政区域地址");
        }
        if (StringUtils.isBlank(cid)) {
            return JsonWrapper.failureWrapperMsg("数据提交异常，请稍后重试");
        }
        return ztcTransportAreaManager.saveZtcArea(code, cid);
    }

//    /**
//     * 根据公司id获取所管理行政区域关系.
//     *
//     * @param cid 公司id
//     * @return
//     */
//    @RequestMapping("getBindArea")
//    @ResponseBody
//    public HashMap<String, Object> getBindArea(String cid) {
//        if (StringUtils.isBlank(cid)) {
//            return JsonWrapper.failureWrapperMsg("数据获取失败,请稍后重试!");
//        }
//        List<ZtcTransportArea> ztcTransportAreas = ztcTransportAreaManager.findBy("transportId", cid);
//        List<SysArea> list = new ArrayList<>();
//        if (ztcTransportAreas.size() > 0) {
//            for (ZtcTransportArea z : ztcTransportAreas) {
//                list.add(sysAreaManager.get(z.getAreaId()));
//            }
//        }
//        List<String> result = new ArrayList<>();
//        if (list.size() > 0) {
//            for (SysArea z : list) {
//                if ("3".equals(String.valueOf(z.getLevels()))) {
//                    result.add(z.getFullName());
//                } else {
//                    result.add(z.getName());
//                }
//            }
//        }
//        return JsonWrapper.successWrapper(result);
//    }

    /**
     * 获取存在关系的省份区域.
     * @return
     */
    @RequestMapping("getExitCompanyOfArea")
    @ResponseBody
    public HashMap<String, Object> getExitCompanyOfArea() {
        List<SysArea> list = ztcTransportAreaManager.getExitCompanyOfArea();
        if (list.isEmpty()) {
            return JsonWrapper.successWrapper("");
        }
        return JsonWrapper.successWrapper(getResult(list));
    }

    /**
     * 获取对应省份或管理的被管理的（地级）省公司(地级公司)信息.
     * @param type  类型,1省公司,2地公司
     * @param areaId  区域id
     * @return
     */
    @RequestMapping("getAreaCompany")
    @ResponseBody
    public HashMap<String, Object> getAreaCompany(String areaId,Integer type) {
        if(type!=1&&type!=2){
            return JsonWrapper.failureWrapperMsg("参数指向性错误！");
        }
        List<ZtcTransport> list = ztcTransportAreaManager.getAreaCompany(areaId,type);
        if (list.isEmpty()) {
            return JsonWrapper.successWrapper("");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("fullName", list.get(0).getFullName());
        map.put("signTel", list.get(0).getSignTel());
        map.put("address", list.get(0).getAddress());
        map.put("time", DateFarmatUtils.format(list.get(0).getSignTime()));
        map.put("id", list.get(0).getId());
        map.put("status", list.get(0).getStatus());
        map.put("email", list.get(0).getEmail());
        map.put("location",sysAreaManager.findUniqueBy("code",list.get(0).getAreaCode()).getFullName());
        map.put("type", getCompanyType(list.get(0).getType()));
        ArrayList<String> result = new ArrayList<>();
        List<ZtcTransportArea> ztcTransportAreas = ztcTransportAreaManager.findBy("transportId", list.get(0).getId());
        List<SysArea> list1 = new ArrayList<>();
        if (ztcTransportAreas.size() > 0) {
            for (ZtcTransportArea z : ztcTransportAreas) {
                list1.add(sysAreaManager.get(z.getAreaId()));
            }
        }
        if (list1.size() > 0) {
            for (SysArea z : list1) {
                if ("3".equals(String.valueOf(z.getLevels()))) {
                    if(!result.contains(z.getFullName())){
                        result.add(z.getFullName());
                    }
                } else {
                    if(!result.contains(z.getName())){
                        result.add(z.getName());
                    }
                }
            }
        }
        String temp="";
        if(result.size()>0){
            for (int i=0;i<result.size();i++){
                if((i+1)==result.size()){
                    temp+=result.get(i);
                }else{
                    temp+=result.get(i)+",";
                }
            }
        }
        map.put("manager", temp);//所管理区域
        return JsonWrapper.successWrapper(map);
    }

    /**
     * 获取对应省份地级市列表信息.
     * @param areaId  区域id
     * @return
     */
    @RequestMapping("getExitCompanyOfAreaList")
    @ResponseBody
    public HashMap<String, Object> getExitCompanyOfAreaList(String areaId) {
        SysArea area = sysAreaManager.get(areaId);
        if (area == null) {
            return JsonWrapper.failureWrapperMsg("网络异常，请稍后重试！");
        }
        if (area.getLevels() != 1) {
            return JsonWrapper.failureWrapperMsg("接口调用错误！");
        }
        return JsonWrapper.successWrapper(getResult(ztcTransportAreaManager.getExitCompanyOfAreaList2(areaId)));
    }

    /**
     * 获取对应省份地级市列表信息.
     * @param areaId  区域id
     * @return
     */
    @RequestMapping("getExitAreaList")
    @ResponseBody
    public HashMap<String, Object> getExitAreaList(String areaId) {
        SysArea area = sysAreaManager.get(areaId);
        List<SysArea> list = new ArrayList<>();
        if (area == null) {
            return JsonWrapper.failureWrapperMsg("网络异常，请稍后重试！");
        }
        if (area.getLevels() != 1) {
            return JsonWrapper.failureWrapperMsg("接口调用错误！");
        }
        if ("北京".equals(area.getName()) || "天津".equals(area.getName()) || "上海".equals(area.getName()) || "重庆".equals(area.getName())) {
            List<SysArea> sysAreas = sysAreaManager.getAreaByPidAndLevel(2, areaId);
            for (SysArea a : sysAreas) {
                list.addAll(sysAreaManager.getAreaByPidAndLevel(3, a.getId()));//list为该省份所有地级市
            }
        } else {
            list = sysAreaManager.getAreaByPidAndLevel(2, areaId);
        }
        return JsonWrapper.successWrapper(getResult(list));
    }


    //区域字段封装
    private List<HashMap<String, Object>> getResult(List<SysArea> list) {
        HashMap<String, Object> result = null;
        List<HashMap<String, Object>> resultList = new ArrayList<>();
        if (list.size() > 0) {
            for (SysArea a : list) {
                result = new HashMap<>();
                result.put("id", a.getId());
                result.put("code", a.getCode());
                result.put("name", a.getName());
                resultList.add(result);
            }
        }
        return resultList;
    }

    //公司类型
    private String getCompanyType(Integer type) {
        String temp = "";
        switch (type) {
            case 1: temp = "省级公司";break;
            case 2: temp = "地级公司";break;
            case 3: temp = "运营公司";break;
            case 4: temp = "车方公司";break;
        }
        return temp;
    }
}